#!/bin/sh

#
# Test that having a long meta.conf doesn't break anything
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource abc {
	host-path = "${TESTFILE_1K}"
}
file-resource def {
	host-path = "${TESTFILE_1K}"
}
file-resource ghi {
        host-path = "${TESTFILE_1K}"
}

task complete {
        on-resource abc { raw_write(0) }
        on-resource def { raw_write(4) }
        on-resource ghi { raw_write(8) }
}
EOF

# Pad it substantially
i=0
while [ $i -lt 200 ]; do
    cat >>$CONFIG << EOF
task complete$i {
        on-resource abc { raw_write(0) }
        on-resource def { raw_write(4) }
        on-resource ghi { raw_write(8) }
}
EOF
    i=$(expr $i + 1)
done

cat >$EXPECTED_META_CONF <<EOF
file-resource "abc" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
file-resource "def" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
file-resource "ghi" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
task "complete" {
  on-resource "abc" {
    funlist = {"2", "raw_write", "0"}
  }
  on-resource "def" {
    funlist = {"2", "raw_write", "4"}
  }
  on-resource "ghi" {
    funlist = {"2", "raw_write", "8"}
  }
}
EOF
i=0
while [ $i -lt 200 ]; do
    cat >>$EXPECTED_META_CONF << EOF
task "complete$i" {
  on-resource "abc" {
    funlist = {"2", "raw_write", "0"}
  }
  on-resource "def" {
    funlist = {"2", "raw_write", "4"}
  }
  on-resource "ghi" {
    funlist = {"2", "raw_write", "8"}
  }
}
EOF
    i=$(expr $i + 1)
done


$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Check that the zip file was created as expected
check_meta_conf

# Check that applying the firmware does the expected thing
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
cmp_bytes 1024 $TESTFILE_1K $IMGFILE 0 0
cmp_bytes 1024 $TESTFILE_1K $IMGFILE 0 2048
cmp_bytes 1024 $TESTFILE_1K $IMGFILE 0 4096
